package com.heo.pojo;

import java.util.Date;
import java.util.concurrent.ConcurrentHashMap;

import com.heo.pub.DictEnum;

/**
 * 用于存放在redis里的设备信息
 * 
 * @author chichi
 */
public class ControlInfoEntity {
	/**
	 * 设备的IMEI号
	 */
	private String deviceIMEI;
	/**
	 * 设备的信号强度，默认0x00表示没有强度信息
	 */
	private int signal = 0x00;
	/**
	 * 设备类型
	 */
	private Short deviceType;

	/**
	 * 向设备发送命令的url地址
	 */
	private String controlUrl;
	/**
	 * 当前与该设备通讯的SocketChannelId
	 */
	private String socketChannelId;

	/**
	 * 设备的通道状态集合
	 */
	private ConcurrentHashMap<Integer, ChannelInfo> channelInfoMap = new ConcurrentHashMap<Integer, ChannelInfo>();

	public void addChannelInfo(Integer channelId, byte channelStatus) {
		ChannelInfo channelInfo = new ChannelInfo();
		channelInfo.setChannelId(channelId);
		channelInfo.setChannelStatus(channelStatus);
		channelInfo.setRefreshTime(new Date().getTime());
		channelInfoMap.put(channelId, channelInfo);
	}

	/**
	 * 设置通道状态 非原子操作，需要做同步
	 * 
	 * @param channelId
	 * @param channelStatus
	 */
	public synchronized void setChannelStatus(Integer channelId, byte channelStatus) {
		ChannelInfo channelInfo = channelInfoMap.get(channelId);
		if (channelInfo == null) {
			channelInfo = new ChannelInfo();
			channelInfo.setChannelId(channelId);
			channelInfo.setChannelStatus(channelStatus);
			channelInfo.setErrorCode("");
			channelInfo.setErrorText("");
			channelInfo.setRefreshTime(new Date().getTime());
		} else {
			channelInfo.setChannelStatus(channelStatus);
			channelInfo.setErrorCode("");
			channelInfo.setErrorText("");
			channelInfo.setRefreshTime(new Date().getTime());
		}
		channelInfoMap.put(channelId, channelInfo);
	}
	
	/**
	 * 设置通道状态 非原子操作，需要做同步
	 * 
	 * @param channelId
	 * @param channelStatus
	 */
	public synchronized void setChannelErrorStatus(Integer channelId, byte channelStatus, String errorCode, String errorText) {
		ChannelInfo channelInfo = channelInfoMap.get(channelId);
		if (channelInfo == null) {
			channelInfo = new ChannelInfo();
			channelInfo.setChannelId(channelId);
			channelInfo.setChannelStatus(channelStatus);
			channelInfo.setErrorCode(errorCode);
			channelInfo.setErrorText(errorText);
			channelInfo.setRefreshTime(new Date().getTime());
		} else {
			channelInfo.setChannelStatus(channelStatus);
			channelInfo.setErrorCode(errorCode);
			channelInfo.setErrorText(errorText);
			channelInfo.setRefreshTime(new Date().getTime());
		}
		channelInfoMap.put(channelId, channelInfo);
	}

	public synchronized byte getChannelStatus(Integer channelId) {
		ChannelInfo channelInfo = channelInfoMap.get(channelId);
		if (channelInfo == null) {
			return DictEnum.DeviceChannelStatusCode.DEVICE_ERROR;
		} else {
			return channelInfo.getChannelStatus();
		}
	}

	private static class ChannelInfo {
		/**
		 * 设备的通道号
		 */
		private Integer channelId;
		/**
		 * 当前通道的工作状态
		 */
		private byte channelStatus = DictEnum.DeviceChannelStatusCode.FREE;// 空闲状态

		/**
		 * 状态刷新的时间
		 */
		private long refreshTime = 0;
		
		private String errorCode;

		private String errorText;

		public Integer getChannelId() {
			return channelId;
		}

		public void setChannelId(Integer channelId) {
			this.channelId = channelId;
		}

		public byte getChannelStatus() {
			return channelStatus;
		}

		public void setChannelStatus(byte channelStatus) {
			this.channelStatus = channelStatus;
		}

		public long getRefreshTime() {
			return refreshTime;
		}

		public void setRefreshTime(long refreshTime) {
			this.refreshTime = refreshTime;
		}

		public String getErrorCode() {
			return errorCode;
		}

		public void setErrorCode(String errorCode) {
			this.errorCode = errorCode;
		}

		public String getErrorText() {
			return errorText;
		}

		public void setErrorText(String errorText) {
			this.errorText = errorText;
		}
	}

	public String getDeviceIMEI() {
		return deviceIMEI;
	}

	public void setDeviceIMEI(String deviceIMEI) {
		this.deviceIMEI = deviceIMEI;
	}

	public Short getDeviceType() {
		return deviceType;
	}

	public void setDeviceType(Short deviceType) {
		this.deviceType = deviceType;
	}

	public String getControlUrl() {
		return controlUrl;
	}

	public void setControlUrl(String controlUrl) {
		this.controlUrl = controlUrl;
	}

	public String getSocketChannelId() {
		return socketChannelId;
	}

	public void setSocketChannelId(String socketChannelId) {
		this.socketChannelId = socketChannelId;
	}

	public ConcurrentHashMap<Integer, ChannelInfo> getChannelInfoMap() {
		return channelInfoMap;
	}

	public void setChannelInfoMap(ConcurrentHashMap<Integer, ChannelInfo> channelInfoMap) {
		this.channelInfoMap = channelInfoMap;
	}

	public int getSignal() {
		return signal;
	}

	public void setSignal(int signal) {
		this.signal = signal;
	}

}
